const fs = require('fs');
const path = require('path');
const qiniu = require('qiniu');

//上传到七牛后保存的文件名
const fileNames = fs.readdirSync(path.join(__dirname, './dist/sole/'));
const filename_0 = fileNames.filter(item => /0\..*\.js/.test(item))[0];
const filename_1 = fileNames.filter(item => /1\..*\.js/.test(item))[0];
//上传到七牛后保存的文件名
const styleFileNames = fs.readdirSync(path.join(__dirname, './dist/sole/js/'));
const style_filename_redirect = styleFileNames.filter(item => /redirect\..*\.js/.test(item))[0];
const style_filename_shop = styleFileNames.filter(item => /shop\..*\.js/.test(item))[0];

var accessKey = 'RZmWV1PvCF872j1_lyG8vbftOxQo3sJnQf2-qWIJ';
var secretKey = 'pqgolVF366d9Pk0noQz-B7IfyHjQ_yRHrBfy8CWa';
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
//要上传的空间
const bucket = 'resource';
var options = {
    scope: bucket,
};
var putPolicy = new qiniu.rs.PutPolicy(options);
var uploadToken=putPolicy.uploadToken(mac);
// qiniu 服务端直传
var config = new qiniu.conf.Config();
// 空间对应的机房
config.zone = qiniu.zone.Zone_z0; // 华东
var localFile_0 = path.join(__dirname, `./dist/sole/${filename_0}`);
var localFile_1 = path.join(__dirname, `./dist/sole/${filename_1}`);
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var key_0 = filename_0;
var key_1 = filename_1;
// 文件上传
const uploadFile = function (key, file) {
    formUploader.putFile(uploadToken, key, file, putExtra, function(respErr,
        respBody, respInfo) {
        if (respErr) {
            throw respErr;
        }
        if (respInfo.statusCode == 200) {
            console.log(respBody);
        } else {
            console.log(respInfo.statusCode);
            console.log(respBody);
        }
    });
};
// 处理css样式文件
const localFile_redirect = path.join(__dirname, `./dist/sole/js/${style_filename_redirect}`);
const localFile_shop = path.join(__dirname, `./dist/sole/js/${style_filename_shop}`);

uploadFile(key_0, localFile_0);
uploadFile(key_1, localFile_1);

// 上传css文件
uploadFile(style_filename_redirect, localFile_redirect);
uploadFile(style_filename_shop, localFile_shop);

const changeValue = function (name, commonJSArr, jsFileNames) {
    const htmlFilePath = path.join(__dirname, `./dist/${name}`);
    let fileContent = fs.readFileSync(htmlFilePath, 'utf8');
    for (let i = 0; i < commonJSArr.length; i++) {
        fileContent = fileContent.replace(`/sole/${commonJSArr[i]}`, `https://resource.bucket.3000ke.cn/${commonJSArr[i]}`);
    }
    for (let i = 0; i < jsFileNames.length; i++) {
        fileContent = fileContent.replace(`/sole/js/${jsFileNames[i]}`, `https://resource.bucket.3000ke.cn/${jsFileNames[i]}`);
    }
    fs.writeFileSync(path.join(__dirname, `./dist/${name}`), fileContent, 'utf8');
};

fs.readdir(path.join(__dirname, './dist/'), 'utf8', function (err, data) {
    if (err) console.log(err);
    for (let i = 0; i < data.length; i++) {
        const item = data[i];
        if (/\.html/g.test(item)) {
            console.log(item);
            changeValue(item, [filename_0, filename_1], [style_filename_redirect, style_filename_shop]);
        }
    }
});
